﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using DevExpress.Web.ASPxTreeList;

public partial class Function : Frame.Pages.BasePage
{

    protected override void Page_Load(object sender, EventArgs e)
    {
        //if (Session["username"] == null)
        //{
        //    Response.Redirect("Login.aspx");
        //}
        //显示数据treelist
        base.Page_Load(sender, e);
        tvShow();
    }
    //数据查询到treelist中
    void tvShow()
    {
        DataTable dt = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable("select * from css_yhqx");
        this.DxTreelist.DataSource = dt;
        this.DxTreelist.DataBind();
    }
    protected override string ActionID
    {
        get { return "0"; }
    }

    //实现选择不同的人员显示当前人员的权限信息
    protected void DxTreelist_CustomCallback(object sender, DevExpress.Web.ASPxTreeList.TreeListCustomCallbackEventArgs e)
    {
        string[] Rights;
        //选择不同人员后刷新当前人员的权组信息
        string right = Frame.Global.GlobalSqlHelper.ExcuteSQLScalar("select qx from css_yhxx where yhbh =" + e.Argument).ToString();
        Rights = right.Split('|');
        //清除所有选择
        for (int i = 0; i < this.DxTreelist.Nodes.Count; i++)
        {
            this.DxTreelist.Nodes[i].Selected = false;
        }

        //确定是否有权限 显示权限
        if (Rights.Length > 0)
        {
            for (int i = 0; i < Rights.Length; i++)
            {
                if (!Rights[i].Equals(""))
                {
                    TreeListNode selection = DxTreelist.FindNodeByKeyValue(Rights[i]);    //查找有权限的项
                    //如果有权限则设置为true
                    if (selection != null)
                    {
                        selection.Selected = true;
                    }
                }
            }
        }
    }

    //实现修改并保存当前人员的权限信息
    protected void QueryCBPanel_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
    {
        //得到从客户端传递过来的参数来进行异步通信响应
        if (e.Parameter.Equals("null"))
        {
            throw new Exception("请选择人员！");
        }
        string right = "";
        if (this.DxTreelist.GetSelectedNodes().Count > 0)   //判断treelist是否勾选了权限信息
        {
            List<TreeListNode> selection = this.DxTreelist.GetSelectedNodes();   //获取选定的权限信息
            //遍历node并连接成字符串
            foreach (TreeListNode node in selection)
            {
                right = right + node.GetValue("qxbm") + "|";
            }
        }

        //实现修改指定人员的权限信息并保存
        List<SqlParameter> Params = new List<SqlParameter>();
        Params.Add(new SqlParameter("@ID", e.Parameter));
        Params.Add(new SqlParameter("@right", right));
        Frame.Global.GlobalSqlHelper.ExcuteSQLScalar("update css_yhxx set qx=@right where yhbh=@ID", Params.ToArray());
    }

}